﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using VAdvantage.Logging;
using VAdvantage.Panels;
using VAdvantage.Grids;

namespace VAdvantage.Controls
{
    public class VTabbedPane :VTabControl,IDisposable  
    {

        /// <summary>
        /// Std Constructor
        /// </summary>
        /// <param name="isWorkBench"></param>
        public VTabbedPane(bool isWorkBench)
        {
            SetWorkbench(isWorkBench);
            this.IsTabStop = false;

        }

        

        public VTabbedPane():this(false)
        {
        }


        /** Workbench 				*/
        private bool _workbenchTab = false;
        /** List of dependent Variables		*/
        private List<String> _dependents = new List<String>();
        /** Disabled Icon			*/
        
        int _oldTabIndex = 0;

        //public event  TabControlEventHandler onSelected;


        /// <summary>
        /// ToString
        /// </summary>
        /// <returns></returns>
        public override String ToString()
        {
            return (_workbenchTab ? "WorkbenchTab" : "WindowTab")
                + " - selected " + this.SelectedIndex + " of " + this.Items.Count;
        }

        public void SetWorkbench(bool isWorkbench)
        {
            _workbenchTab = isWorkbench;
            //if (_workbenchTab)
            //    super.setTabPlacement(JTabbedPane.BOTTOM);
            //else
            //    super.setTabPlacement(Language.getLoginLanguage().isLeftToRight()
            //        ? JTabbedPane.LEFT : JTabbedPane.RIGHT);
        }   //  setW

        /// <summary>
        ///Tab is Workbench (not Window)
        /// </summary>
        /// <returns></returns>
        public bool IsWorkbench()
        {
            return _workbenchTab;
        }

        /// <summary>
        ///Evaluate Tab Logic
        /// </summary>
        /// <param name="e"></param>
        public void Evaluate(String e)
        {
            bool process = e == null;
            String columnName = null;
            if (!process)
            {
                columnName = e;
                if (columnName != null)
                    process = _dependents.Contains(columnName);
                else
                    process = true;
            }

            if (process)
            {
                VLogger.Get().Config(columnName == null ? "" : columnName);
                for (int i = 0; i < Items.Count; i++)
                {
                    Control c = (Control)((TabItem)Items[i]).Content;// getComponentAt(i);
                    if (c is GridController)
                    {
                        GridController gc = (GridController)c;
                        bool display =  gc.IsDisplayed();
                        //setEnabledAt(i, display);
                        ((VTabItem)Items[i]).IsEnabled = display;
                        //if (display)
                        //{
                        //    TabPages[i].ImageKey = "";
                        //}
                        //else
                        //{
                        //    TabPages[i].ImageKey = "disabled";
                        //}
                    }
                }
            }
        }

        /// <summary>
        ///	Add Tab
        /// </summary>
        /// <param name="tabName">tabName name</param>
        /// <param name="gTab">gTab grid tab model</param>
        /// <param name="tabElement">tabElement GridController or VSortTab</param>
        public void AddTab(String tabName, GridTab gTab, Control tabElement)
        {
            int index = this.Items.Count;

            VTabItem tp = new VTabItem();
            tp.Index = index;
            tp.Text = tabName;
            
            tp.Selecting += OnSelecting;
           // tp.ToolTipText = gTab.GetDescription();
           // tp.BackColor = DataBase.GlobalVariable.WINDOW_BACK_COLOR;
            tabElement.Margin = new Thickness(-5);
            tp.Content  = tabElement;

            this.Items.Add(tp);

            //if (this.TabCount == 1)
            //{
            //    _oldTabIndex = 0;
            //    _imgList.Images.Add("disabled", Utility.Env.GetImageIcon("Cancel10.gif"));
            //    //onSelected(this,new TabControlEventArgs(this.TabPage[0],0,TabControlAction.Selected);
            //}

            //super.addTab(tabName, gTab.getIcon(),
            //    tabElement, gTab.getDescription());
            List<String> dependents = gTab.GetDependentOn();
            for (int i = 0; i < dependents.Count; i++)
            {
                String name = dependents[i];
                if (!_dependents.Contains(name))
                    _dependents.Add(name);
            }
            //if (s_disabledIcon == null)
            //    s_disabledIcon = Env.getImageIcon("Cancel10.gif");
            //setDisabledIconAt(index, s_disabledIcon);
        }	//	addTab

        /// <summary>
        /// Tab Selecting event
        /// </summary>
        /// <param name="e"></param>
        protected  void OnSelecting(Object sender, MouseButtonEventArgs e)
        {
            VTabItem page = (VTabItem)sender;  
            Control newC = (Control) page.Content;//   getComponentAt(index);
            int index =  page.Index;
            GridController newGC = null;
            if (newC is GridController)
                newGC = (GridController)newC;
            //	Display
            if (newGC != null)
            {
                bool display = newGC.IsDisplayed(); // if tab is not displayed
                if (!display)
                {
                    //VLogger.Get().Info("Not displayed - " + newGC.ToString());
                    e.Handled = true;
                    return;
                }
            }
            //
            int oldIndex = _oldTabIndex;
            if (newGC != null && oldIndex >= 0 && index != oldIndex)
            {
                Control oldC = (Control) ((TabItem)this.Items[oldIndex]).Content ;//.Controls[0];
                if (oldC != null && oldC is GridController)
                {
                    GridController oldGC = (GridController)oldC;
                    /* check for tab Level of tab */
                    if (newGC.GetTabLevel() > oldGC.GetTabLevel() + 1)
                    {
                        //	Search for right tab
                        for (int i = index - 1; i >= 0; i--)
                        {
                            Control rightC = (Control)((TabItem)this.Items[i]).Content;// .Controls[0];// getComponentAt(i);
                            GridController rightGC = null;
                            if (rightC is GridController)
                            {
                                rightGC = (GridController)rightC;
                                if (rightGC.GetTabLevel() == oldGC.GetTabLevel() + 1)
                                {
                                   Classes.ShowMessage.Info("TabSwitchJumpGo", true,rightGC.GetTitle(),"");
                                    //ADialog.warn(0, this, "TabSwitchJumpGo", rightGC.getTitle());
                                    e.Handled  = true;
                                    return;
                                }
                            }
                        }
                        Classes.ShowMessage.Warn("TabSwitchJumpGo", null, "", "");
                        //ADialog.warn(0, this, "TabSwitchJump");
                        //MessageBox.Show("TabSwitchJump");
                        e.Handled = true;
                        return;
                    }
                    oldGC.SetMnemonics(false);
                }
            }
            //	Switch
            if (newGC != null)
            {
                newGC.SetMnemonics(true);
            }
            _oldTabIndex = page.Index;
            //base.OnSelecting(e);
            // TabPages[oldIndex].Show();
            //TabPages[oldIndex].BringToFront();

        }   


        public void Dispose()
        {
            if (_dependents != null)
                _dependents.Clear();
            _dependents = null;

            foreach (VTabItem item in this.Items)
            {
                item.Dispose();
            }
            this.Items.Clear();
        }
    }
}
